package cn.edu.neusoft.neufooddemo.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.neufooddemo.R;
import cn.edu.neusoft.neufooddemo.adapter.FoodInfoAdapter;
import cn.edu.neusoft.neufooddemo.bean.CollectStatus;
import cn.edu.neusoft.neufooddemo.bean.CommentInfo;
import cn.edu.neusoft.neufooddemo.bean.FoodInfo;
import cn.edu.neusoft.neufooddemo.bean.Result;
import cn.edu.neusoft.neufooddemo.common.Constants;
import cn.edu.neusoft.neufooddemo.listener.RetrofitListener;
import cn.edu.neusoft.neufooddemo.model.FoodModel;
import cn.edu.neusoft.neufooddemo.model.UserModel;

public class FoodInfoActivity extends BaseActivity implements RetrofitListener{
    private Context mContext;
    private String food_id;
    private ImageView foodinfo_pic,foodinfo_bottom_img;
//    private ImageView foodinfo_pic,foodinfo_bottom_img,foodinfo_return;
   private Toolbar foodinfo_foodname;
    private TextView foodinfo_intro,foodinfo_price,foodinfo_bottom_collect;
    private RecyclerView foodinfo_comments;
    private LinearLayout foodinfo_collect,foodinfo_purchase,foodinfo_call;
    private String result;
    private FoodInfo foodinfo;
    private FoodInfoAdapter adapter;
    private FoodModel foodModel;
    private UserModel userModel;
    private String phonenum;

     protected void initViews(){
       //  setLayout(R.layout.activity_foodinfo3);
        foodinfo_pic = (ImageView)findViewById(R.id.foodinfo_pic);
        foodinfo_bottom_img = (ImageView)findViewById(R.id.foodinfo_bottom_img);
      //   foodinfo_foodname = (Toolbar)findViewById(R.id.foodinfo_name);
        foodinfo_intro = (TextView)findViewById(R.id.foodinfo_intro);
        foodinfo_price = (TextView)findViewById(R.id.foodinfo_price);
        foodinfo_bottom_collect = (TextView)findViewById(R.id.foodinfo_bottom_collect);
        foodinfo_comments = (RecyclerView)findViewById(R.id.foodinfo_comments);
        foodinfo_comments.setLayoutManager(new LinearLayoutManager(FoodInfoActivity.this));
         foodinfo_comments.setNestedScrollingEnabled(false);

//        foodinfo_return = (ImageView)findViewById(R.id.foodinfo_return);
        foodinfo_collect = (LinearLayout)findViewById(R.id.foodinfo_collect);
        foodinfo_purchase = (LinearLayout)findViewById(R.id.foodinfo_purchase);
        foodinfo_call = (LinearLayout)findViewById(R.id.food_call);
        mContext=this;
    }

     void initEvents(){
//        foodinfo_return.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                finish();
//            }
//        });
        foodinfo_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collect();
            }
        });
        foodinfo_purchase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FoodInfoActivity.this,PurchaseActivity.class);
                intent.putExtra("shopid",foodinfo.getShop_id());
                intent.putExtra("foodid",foodinfo.getFood_id());
                intent.putExtra("foodname",foodinfo.getFoodname());
                intent.putExtra("price",foodinfo.getPrice());
                foodinfo_foodname.setTitle(foodinfo.getFoodname());
                startActivity(intent);
            }
        });
        foodinfo_call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("tel:" + phonenum);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                if (checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    startActivity(intent);
                    return;
                }
            }
        });
    }

     void initData(){
        Intent intent=getIntent();
        food_id =intent.getStringExtra("id");
        phonenum =intent.getStringExtra("phonenum");
        showFoodInfo(food_id);
        showComments();
        iscollected();
    }
    private void showFoodInfo(String food_id)
    {
        foodModel=new FoodModel();
        foodModel.getFoodById(food_id,this);
    }
    private void showComments(){
        foodModel=new FoodModel();
       // foodModel.getAllUserFoodOrder(food_id,this);
        foodModel.getAllCommentsByFood(food_id,this);

    }

    private void iscollected(){
        foodModel=new FoodModel();
        foodModel.isCollected(getUser_id(), food_id,"1",this);
    }

    private void collect(){
        userModel = new UserModel();
        userModel.userCollectFood(getUser_id(), food_id,this);
    }

    @Override
    public void onSuccess(Object o, int flag) {
        switch (flag)
        {
            case Constants.GETFOODBYID:
                foodinfo=(FoodInfo) o;
                if(!foodinfo.getPic().equals("")){
                    Picasso.get().load(Constants.BASE_URL+foodinfo.getPic()).into(foodinfo_pic);
                }
                foodinfo_foodname.setTitle(foodinfo.getFoodname());
                foodinfo_intro.setText("简介："+foodinfo.getIntro());
                foodinfo_price.setText("￥"+foodinfo.getPrice()+"元");
                break;
            case Constants.GETALLUSERFOODORDER:
                adapter = new FoodInfoAdapter(FoodInfoActivity.this, (List<CommentInfo>) o, R.layout.cardview_foodinfo_comment);
                foodinfo_comments.setAdapter(adapter);
                break;
            case Constants.ISCOLLECTED:
                result=((CollectStatus)o).getCollected().toString();
                if (result.equals("0")) {
                    foodinfo_bottom_img.setImageResource(R.drawable.tab_collect_normal);
                    foodinfo_bottom_collect.setText("收藏");
                    foodinfo_bottom_collect.setTextColor(getResources().getColor(R.color.normal_themecolor));
                } else {
                    foodinfo_bottom_img.setImageResource(R.drawable.tab_collect_pressed);
                    foodinfo_bottom_collect.setText("取消收藏");
                    foodinfo_bottom_collect.setTextColor(getResources().getColor(R.color.themecolor));
                }
                break;
            case Constants.USERCOLLECTFOOD:
                if (((Result)o).getSuccess().equals("1")) {
                    if (result.equals("0")) {
                        foodinfo_bottom_img.setImageResource(R.drawable.tab_collect_pressed);
                        foodinfo_bottom_collect.setText("取消收藏");
                        foodinfo_bottom_collect.setTextColor(getResources().getColor(R.color.themecolor));
                        Toast.makeText(FoodInfoActivity.this,"收藏成功", Toast.LENGTH_SHORT).show();
                        result = "1";
                    } else {
                        foodinfo_bottom_img.setImageResource(R.drawable.tab_collect_normal);
                        foodinfo_bottom_collect.setText("收藏");
                        foodinfo_bottom_collect.setTextColor(getResources().getColor(R.color.normal_themecolor));
                        showToast("取消收藏成功");
                        result = "0";
                    }
                } else {
                    showToast("操作失败");
                }
                break;
        }

    }

    @Override
    public void onFail() {
        showToast("网络连接错误");
    }
}
